package com.ruoyi.system.domain.tree;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.tree.CommonTree;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.Date;
import java.util.Objects;

/**
 * 描述
 * 作者 ZhangPing
 * 创建时间 2023/10/7 17:13
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
@Accessors(chain = true)
public class DeptTree extends CommonTree<DeptTree> {

    private String shortName;
    private String address;
    private String remark;
    private int vehicleCount;
    private Date createTime;
    private Date updateTime;

    public DeptTree(SysDept dept){
        super(dept);
        this.shortName = dept.getShortName();
        this.address = dept.getAddress();
        this.remark = dept.getRemark();
        this.vehicleCount = dept.getVehicleCount();
        this.createTime = dept.getCreateTime();
        this.updateTime = dept.getCreateTime();
    }

    /**
     * 计算车辆总数
     * @param node
     * @return
     */
    public static DeptTree sumVehicleCount(DeptTree node){
        if (!node.getChildren().isEmpty()){
            int count = node.getVehicleCount();
            for (DeptTree child : node.getChildren()) {
                count += sumVehicleCount(child).getVehicleCount();
            }
            node.setVehicleCount(count);
        }
        return node;
    }

    /**
     * 转换为SysDept
     * @return
     */
    public SysDept toSysDept(){
        return new SysDept()
                .setCreateTime(getCreateTime())
                .setUpdateTime(getUpdateTime())
                .toExtentEntity(SysDept.class)
                .setDeptId(Long.parseLong(id()))
                .setDeptName(label())
                .setShortName(getShortName())
                .setAddress(getAddress())
                .setVehicleCount(getVehicleCount());
    }
}
